#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : bigDataProject 
@File    : pos_setup_api.py
@IDE     : PyCharm 
@Author  : Mario
@Date    : 2022/11/14 14:37 
@Describe: 中台pos设置相关接口
"""
from typing import Text

from api.middle_api.base_middle_api import BaseMiddleAPI
from utils.common.common_control import get_shop_code
from utils.time.time_control import get_now_time


class PosSetupAPI(BaseMiddleAPI):

    def __init__(self, env: Text, brand_code: Text) -> None:
        super().__init__(env)
        self.brand_code = brand_code

    def pos_device_manage_page(self, device_mac='e4:54:e8:c2:f1:26', page_num='1', page_size='100'):
        """
        设备管理分页
        :param device_mac: 设备MAC地址
        :param page_num: 第几页，默认1
        :param page_size: 每页展示几条，默认100
        :return:
        """
        payload = '{"pageNum":"' + page_num + '","pageSize":"' + page_size + '","brandCode":"' + self.brand_code + \
                  '","deviceMac":"' + device_mac + '"}'
        response = self.http_request('catering-backend/pos/device/manage/page', payload, "中台设备管理分页")
        return response

    def shop_application_event_insert(self, event_type='21', application_name='print-api'):
        """
        推送门店应用事件
        :param event_type: 应用类型: 21-重启
        :param application_name: 服务名称：pos-api ， print-api
        :return:
        """
        shop_code = get_shop_code(self.brand_code)
        now_time = get_now_time()
        payload = '{"brandCode":"' + self.brand_code + '","applicationName":"' + application_name + '","eventType":"' + event_type + '","targetVersion":"","shopCodes":["' + shop_code + '"],"executeTime":"' + now_time + '"}'
        response = self.http_request('shop-steward-backend/shopApplicationEvent/insert', payload,
                                     "中台推送门店应用事件")
        return response

    def pos_derive_update_status(self, derive_id, device_status='0'):
        """
        更新设备状态
        :param derive_id: 设备ID,通过设备管理分页接口获取
        :param device_status: 设备状态: 0-关闭，1-开启
        :return:
        """
        payload = '{"brandCode":"' + self.brand_code + '","deviceStatus":"' + device_status + '","id":"' + derive_id + '"}'
        response = self.http_request('catering-backend/pos/device/manage/updateStatusById', payload, "中台更新设备状态")
        return response

    def pos_device_manage_dict_list_device_brand(self):
        """
        获取设备品牌字典
        :return:
        """
        payload = '{}'
        response = self.http_request('catering-backend/pos/device/manage/dicList/deviceBrand', payload,
                                     "中台获取设备品牌字典")
        return response

    def pos_printer_manage_printer_type_dict(self):
        """
        打印机类型字典列表
        :return:
        """
        payload = '{}'
        response = self.http_request('catering-backend/pos/printer/manage/printerTypeDic', payload,
                                     "中台打印机类型字典列表")
        return response

    def pos_device_manage_save(self, device_brand_code, device_name, device_mac, printer_ip,
                               device_status, cash_code, printer_type_code, device_brand_name):
        """
        POS设备新增
        :param device_brand_code: 设备品牌编码
        :param device_name: 设备名称
        :param device_mac: 设备mac地址
        :param printer_ip: ip地址
        :param device_status: 设备状态(0-禁用 1-启用)
        :param cash_code: 收银机号
        :param printer_type_code: 打印机型号名称编码
        :param device_brand_name: 设备品牌名称
        :return:
        """
        shop_code = get_shop_code(self.brand_code)
        payload = '{"printerMode":"3","brandCode":"' + self.brand_code + '","shopCode":"' + shop_code + '","deviceBrandCode":"' + device_brand_code + '","deviceName":"' + device_name + '","deviceMac":"' + device_mac + '","printerIp":"' + printer_ip + '","deviceType":"1","deviceStatus":"' + device_status + '","cashCode":"' + cash_code + '","printerTypeCode":"' + printer_type_code + '","receiptPrint":"11111111","deviceBrandName":"' + device_brand_name + '","softwareVersion":"V20200204-01"}'
        response = self.http_request('catering-backend/pos/device/manage/save', payload, "中台POS设备新增")
        return response


if __name__ == '__main__':
    test = PosSetupAPI("sit", "823882")
    test.pos_printer_manage_printer_type_dict()
